class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def mergeNodes(self, head):
        ans = 0
        mut_pre = ListNode(0)
        pre = mut_pre
        node = head
        while node:
            if node.val == 0:
                if ans:
                    new_node = ListNode(ans)
                    pre.next = new_node
                    pre = pre.next
                    ans = 0
            else:
                ans += node.val
            node = node.next
        return mut_pre.next
